From 53d47f981ea867f010744535685c4f62120d79e2 Mon Sep 17 00:00:00 2001 From: oliskoli Date: Sat, 14 Jul 2007 21:04:38 +0000 Subject: [PATCH] garmin_fs: Improve support for Garmin address fields. --- gpsbabel/garmin_fs.c | 143 +++++++++++++++++++++++++++++++++---------- gpsbabel/garmin_fs.h | 10 ++- 2 files changed, 121 insertions(+), 32 deletions(-) diff --git a/gpsbabel/garmin_fs.c b/gpsbabel/garmin_fs.c index f20e0e9f4..3fa634eb4 100644 --- a/gpsbabel/garmin_fs.c +++ b/gpsbabel/garmin_fs.c @@ -27,6 +27,13 @@ #define MYNAME "garmin_fs" +#define GARMIN_GPX_EXT_REFERENCE \ + "xmlns:gpxx=\"" \ + "http://www.garmin.com/xmlschemas/GpxExtensions/v3\" " \ + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \ + "xsi:schemaLocation=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3 " \ + "http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd" + garmin_fs_t * garmin_fs_alloc(const int protocol) { @@ -36,7 +43,7 @@ garmin_fs_alloc(const int protocol) result->fs.type = FS_GMSD; result->fs.copy = (fs_copy) garmin_fs_copy; result->fs.destroy = garmin_fs_destroy; - result->fs.convert = NULL; + result->fs.convert = garmin_fs_convert; result->fs.next = NULL; result->protocol = protocol; @@ -52,11 +59,16 @@ garmin_fs_destroy(void *fs) { garmin_ilink_t *ilinks; + if (data->addr != NULL) xfree(data->addr); + if (data->cc != NULL) xfree(data->cc); if (data->city != NULL) xfree(data->city); + if (data->country != NULL) xfree(data->country); + if (data->cross_road != NULL) xfree(data->cross_road); if (data->facility != NULL) xfree(data->facility); + if (data->phone_nr != NULL) xfree(data->phone_nr); + if (data->postal_code != NULL) xfree(data->postal_code); if (data->state != NULL) xfree(data->state); - if (data->cc != NULL) xfree(data->cc); - if (data->cross_road != NULL) xfree(data->cross_road); + if ((ilinks = data->ilinks) != NULL) { ilinks->ref_count--; if (ilinks->ref_count <= 0) { @@ -85,12 +97,30 @@ void garmin_fs_copy(garmin_fs_t **dest, garmin_fs_t *src) memcpy(*dest, src, sizeof(*src)); - (*dest)->city = (src->city != NULL) ? xstrdup(src->city) : NULL; - (*dest)->facility = (src->facility != NULL) ? xstrdup(src->facility) : NULL; - (*dest)->state = (src->state != NULL) ? xstrdup(src->facility) : NULL; + (*dest)->addr = (src->addr != NULL) ? xstrdup(src->addr) : NULL; (*dest)->cc = (src->cc != NULL) ? xstrdup(src->cc) : NULL; + (*dest)->city = (src->city != NULL) ? xstrdup(src->city) : NULL; + (*dest)->country = (src->country != NULL) ? xstrdup(src->country) : NULL; (*dest)->cross_road = (src->cross_road != NULL) ? xstrdup(src->cross_road) : NULL; - (*dest)->addr = (src->addr != NULL) ? xstrdup(src->addr) : NULL; + (*dest)->facility = (src->facility != NULL) ? xstrdup(src->facility) : NULL; + (*dest)->phone_nr = (src->phone_nr != NULL) ? xstrdup(src->phone_nr) : NULL; + (*dest)->postal_code = (src->postal_code != NULL) ? xstrdup(src->postal_code) : NULL; + (*dest)->state = (src->state != NULL) ? xstrdup(src->state) : NULL; +} + +void garmin_fs_convert(void *fs) +{ + garmin_fs_t *gmsd = (garmin_fs_t *) fs; + + if (gmsd->addr) gmsd->addr = cet_convert_string(gmsd->addr); + if (gmsd->cc) gmsd->cc = cet_convert_string(gmsd->cc); + if (gmsd->city) gmsd->city = cet_convert_string(gmsd->city); + if (gmsd->country) gmsd->country = cet_convert_string(gmsd->country); + if (gmsd->cross_road) gmsd->cross_road = cet_convert_string(gmsd->cross_road); + if (gmsd->facility) gmsd->facility = cet_convert_string(gmsd->facility); + if (gmsd->phone_nr) gmsd->phone_nr = cet_convert_string(gmsd->phone_nr); + if (gmsd->postal_code) gmsd->postal_code = cet_convert_string(gmsd->postal_code); + if (gmsd->state) gmsd->state = cet_convert_string(gmsd->state); } /* GPX - out */ @@ -98,23 +128,30 @@ void garmin_fs_copy(garmin_fs_t **dest, garmin_fs_t *src) void garmin_fs_xml_fprint(gbfile *ofd, const waypoint *waypt) { + char *phone, *addr; garmin_fs_t *gmsd = GMSD_FIND(waypt); + if (gmsd == NULL) return; - if ((gmsd->flags.category && gmsd->category) || - WAYPT_HAS(waypt, depth) || - WAYPT_HAS(waypt, proximity) || - WAYPT_HAS(waypt, temperature) || - gmsd->flags.display) + addr = GMSD_GET(addr, ""); + if (! *addr) addr = GMSD_GET(city, ""); + if (! *addr) addr = GMSD_GET(country, ""); + if (! *addr) addr = GMSD_GET(postal_code, ""); + if (! *addr) addr = GMSD_GET(state, ""); + + phone = GMSD_GET(phone_nr, ""); + + if (*addr || *phone || + (gmsd->flags.category && gmsd->category) || + WAYPT_HAS(waypt, depth) || + WAYPT_HAS(waypt, proximity) || + WAYPT_HAS(waypt, temperature) || + gmsd->flags.display) { int space = 1; gbfprintf(ofd, "%*s\n", space++ * 2, ""); - gbfprintf(ofd, "%*s\n", space++ * 2, ""); + gbfprintf(ofd, "%*s\n", space++ * 2, "", GARMIN_GPX_EXT_REFERENCE); if WAYPT_HAS(waypt, proximity) gbfprintf(ofd, "%*s%.6f\n", space * 2, "", waypt->proximity); if WAYPT_HAS(waypt, temperature) @@ -151,6 +188,27 @@ garmin_fs_xml_fprint(gbfile *ofd, const waypoint *waypt) } gbfprintf(ofd, "%*s\n", --space * 2, ""); } + if (*addr) { + char *str; + gbfprintf(ofd, "%*s\n", space++ * 2, ""); + + if ((str = GMSD_GET(addr, NULL))) + gbfprintf(ofd, "%*s%s\n", space * 2, "", str); + if ((str = GMSD_GET(city, NULL))) + gbfprintf(ofd, "%*s%s\n", space * 2, "", str); + if ((str = GMSD_GET(state, NULL))) + gbfprintf(ofd, "%*s%s\n", space * 2, "", str); + if ((str = GMSD_GET(country, NULL))) + gbfprintf(ofd, "%*s%s\n", space * 2, "", str); + if ((str = GMSD_GET(postal_code, NULL))) + gbfprintf(ofd, "%*s%s\n", space * 2, "", str); + + gbfprintf(ofd, "%*s\n", --space * 2, ""); + } + if (*phone) { + gbfprintf(ofd, "%*s%s\n", space * 2, "", phone); + } + gbfprintf(ofd, "%*s\n", --space * 2, ""); gbfprintf(ofd, "%*s\n", --space * 2, ""); } @@ -170,26 +228,31 @@ garmin_fs_xml_convert(const int base_tag, int tag, const char *cdatastr, waypoin tag -= base_tag; /* - tt_garmin_extension, -> 0 - tt_garmin_waypt_extension, -> 1 - tt_garmin_proximity, -> 2 - tt_garmin_temperature,-> 3 - tt_garmin_depth, -> 4 - tt_garmin_display_mode, -> 5 - tt_garmin_categories, -> 6 - tt_garmin_category, -> 7 + tt_garmin_waypt_extension, -> 0 + tt_garmin_proximity, -> 1 + tt_garmin_temperature,-> 2 + tt_garmin_depth, -> 3 + tt_garmin_display_mode, -> 4 + tt_garmin_categories, -> 5 + tt_garmin_category, -> 6 + tt_garmin_addr, -> 7 + tt_garmin_city, -> 8 + tt_garmin_state, -> 9 + tt_garmin_country, -> 10 + tt_garmin_postal_code, -> 11 + tt_garmin_phone_nr, -> 12 */ switch(tag) { - case 2: + case 1: if (*cdatastr) WAYPT_SET(waypt, proximity, atof(cdatastr)); break; - case 3: + case 2: if (*cdatastr) WAYPT_SET(waypt, temperature, atof(cdatastr)); break; - case 4: + case 3: if (*cdatastr) WAYPT_SET(waypt, depth, atof(cdatastr)); break; - case 5: + case 4: if (case_ignore_strcmp(cdatastr, "SymbolOnly") == 0) { GMSD_SET(display, gt_display_mode_symbol); } @@ -200,9 +263,27 @@ garmin_fs_xml_convert(const int base_tag, int tag, const char *cdatastr, waypoin GMSD_SET(display, gt_display_mode_symbol_and_name); } break; - case 7: if ( ! garmin_fs_merge_category(cdatastr, waypt)) { + case 6: + if ( ! garmin_fs_merge_category(cdatastr, waypt)) warning(MYNAME ": Unable to convert category \"%s \"!\n", cdatastr); - } + break; + case 7: + GMSD_SETSTR(addr, cdatastr); + break; + case 8: + GMSD_SETSTR(city, cdatastr); + break; + case 9: + GMSD_SETSTR(state, cdatastr); + break; + case 10: + GMSD_SETSTR(country, cdatastr); + break; + case 11: + GMSD_SETSTR(postal_code, cdatastr); + break; + case 12: + GMSD_SETSTR(phone_nr, cdatastr); break; } } diff --git a/gpsbabel/garmin_fs.h b/gpsbabel/garmin_fs.h index 7c1cd9ab8..c1999e1d9 100644 --- a/gpsbabel/garmin_fs.h +++ b/gpsbabel/garmin_fs.h @@ -38,6 +38,7 @@ /* macros */ #define GMSD_FIND(a) (garmin_fs_t *) fs_chain_find((a)->fs, FS_GMSD) +#define GMSD_HAS(a) (gmsd && gmsd->flags.a) /* GMSD_GET(a,b): a = any gmsd field, b = default value */ #define GMSD_GET(a,b) ((gmsd) && (gmsd->flags.a)) ? (gmsd->a) : (b) @@ -73,7 +74,10 @@ typedef struct { unsigned int facility:1; unsigned int cc:1; unsigned int cross_road:1; - unsigned int addr:1; + unsigned int addr:1; + unsigned int country:1; + unsigned int phone_nr:1; + unsigned int postal_code:1; #ifdef GMSD_EXPERIMENTAL unsigned int subclass:1; #endif @@ -96,6 +100,9 @@ typedef struct garmin_fs_s char *cc; /* country code */ char *cross_road; /* Intersection road label */ char *addr; /* address + number */ + char *country; /* country */ + char *phone_nr; /* phone number */ + char *postal_code; /* postal code */ garmin_ilink_t *ilinks; #ifdef GMSD_EXPERIMENTAL char subclass[22]; @@ -105,6 +112,7 @@ typedef struct garmin_fs_s garmin_fs_t *garmin_fs_alloc(const int protocol); void garmin_fs_destroy(void *fs); void garmin_fs_copy(garmin_fs_t **dest, garmin_fs_t *src); +void garmin_fs_convert(void *fs); char *garmin_fs_xstrdup(const char *src, size_t size); /* for GPX */ -- 2.30.2